﻿using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using System.Collections.Generic;

namespace Business
{
    public class Funds
    {
        private ISessionFactory _sessionFactory;
        public Funds()
        {
            _sessionFactory = DataObjects.Service.SessionFactory.GetInstance().GetNHibernateFactory();
        }
        
        public ArrayList List()
        {
            ArrayList arrRet = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                arrRet = (ArrayList)session.CreateCriteria(typeof(DataObjects.Funds)).List();
                tx.Commit();
            }
            return arrRet;
        }

        public DataObjects.Funds GetObjectByPK(string type, string unit)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from Funds where Type = ? and Unit = ?").AddEntity(typeof(DataObjects.Funds));
                query.SetString(0, type);
                query.SetString(1, unit);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                if (arrTemp.Count > 0)
                {
                    return (DataObjects.Funds)arrTemp[0];
                }
                return null;
            }
        }

       

        //code insert - update - delete 
        public void Insert(DataObjects.Funds oFunds)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(oFunds);
                tx.Commit();			        
            }
        }
        public void Update(DataObjects.Funds oFunds)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Update(oFunds);
                tx.Commit();		        
            }
        }
        public void Delete(DataObjects.Funds oFunds)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Delete(oFunds);
                tx.Commit();
            }
        }
    }
}
